/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 强仔
 * Date: 2021-09-03
 * Time: 8:30
 */
//超过int范围了
//卧槽卧槽卧槽卧槽
class ListNode{
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
public class Solution {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int a1 = 0;
        ListNode cur1 = l1;
        int count1 = 0;
        while(cur1!=null){
            a1 += cur1.val*Math.pow(10,count1);
            count1++;
            cur1 = cur1.next;
        }
        int a2 = 0;
        ListNode cur2 = l2;
        int count2 = 0;
        while(cur2!=null){
            a2 += cur2.val*Math.pow(10,count2);
            count2++;
            cur2 = cur2.next;
        }
        int a = a1+a2;
        ListNode Numbers = new ListNode(0);
        ListNode cur = Numbers;
        if(a==0){
            cur.val = a;
            return cur;
        }
        while(true){
            int b = a % 10;
            a = a / 10;
            cur.val = b;
            if(a==0){
                return Numbers;
            }
            cur.next = new ListNode(0);
            cur = cur.next;
        }
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(9);
        ListNode l2 = new ListNode(1);
        ListNode set = l2;
        for (int i = 0; i < 9; i++) {
            set.next = new ListNode(9);
            set = set.next;
        }
        ListNode l = addTwoNumbers(l1, l2);
        ListNode cur = l;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }
    public static ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(0);
        ListNode cur = pre;
        int count = 0;
        while(l1 != null || l2 != null){
            int x = l1==null ? 0:l1.val;
            int y = l2==null ? 0:l2.val;
            int sum = x+y+count;
            count = sum/10;
            sum = sum%10;
            cur.next = new ListNode(sum);
            cur = cur.next;
            if(l1!=null){
                l1=l1.next;
            }
            if(l2!=null){
                l2=l2.next;
            }
        }
        if(count==1){
            cur.next = new ListNode(1);
        }
        return pre.next;
    }
}
